package com.wr.springbootRabbitMq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq配置类
 */
@Configuration
public class RabbitMqDirectTTLConfig {

    // 1、声明交换机
    @Bean
    public DirectExchange directExchangeTTL(){
        return new DirectExchange("direct-order-ttl-exchange");
    }

    //2、声明队列
    @Bean
    public Queue smsDirectTTLQueue(){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("x-message-ttl",5000);  // 5秒的过期队列
        map.put("x-dead-letter-exchange","dead-order-exchange");  // 配置死信队列的交换机名
        map.put("x-dead-letter-routing-key","dead"); // 因为死信队列是用的direct模式 则需要key  如果是发布订阅模式则不需要
        return new Queue("sms.direct.ttl.queue",true,false,false,map);
    }

    @Bean
    public Queue smsDirectTTLMessageQueue(){
        return new Queue("sms.direct.ttl.message.queue");
    }

    //3、绑定交换机与队列
    @Bean
    public Binding smsDirectTTLBinding(){
        return BindingBuilder.bind(smsDirectTTLQueue()).to(directExchangeTTL()).with("sms");
    }
    @Bean
    public Binding smsDirectTTLMessageBinding(){
        return BindingBuilder.bind(smsDirectTTLMessageQueue()).to(directExchangeTTL()).with("ttlMessage");
    }

}
